from library_test.base_test import BaseTest


class BaseTestCheckUtil(BaseTest):

    @classmethod
    def get_module_path(cls):
        return "ascend_deployer.module_utils.check_library_utils"

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        from ascend_deployer.module_utils.check_output_manager import CheckOutput, CheckConfig
        config = {
            "check_item": "check_npu_health",
            "desc_en": "Check NPU health status:"
                       "1. Check whether the driver is normal and judge whether the npu-smi info command is successfully executed."
                       "2. Check the npu-smi info echo to determine whether there is a non-OK card in the status.",
            "desc_zh": "检查NPU健康状态："
                       "1、检查驱动是否正常，通过npu-smi info命令是否执行成功判断，"
                       "2、查看npu-smi info回显，判断状态是否有非OK的卡。",
            "tip_en": "Just execute npu-smi info to troubleshoot.",
            "tip_zh": "执行npu-smi info排查即可。",
            "help_url": ""
        }
        check_config = CheckConfig(
            check_item=config.get("check_item", ""),
            desc_en=config.get("desc_en", ""),
            desc_zh=config.get("desc_zh", ""),
            tip_zh=config.get("tip_zh", ""),
            tip_en=config.get("tip_en", ""),
            help_url=config.get("help_url", "")
        )
        cls.check_output = CheckOutput(check_config)


class TestCheckUtils(BaseTestCheckUtil):
    def test_to_json(self):
        self.check_output.error_msg = ['err msg']
        expect_result = {'check_item': 'check_npu_health',
                         'check_status': 'wait',
                         'desc_en': 'Check NPU health status:1. Check whether the driver is normal and '
                                    'judge whether the npu-smi info command is successfully '
                                    'executed.2. Check the npu-smi info echo to determine whether '
                                    'there is a non-OK card in the status.',
                         'desc_zh': '检查NPU健康状态：1、检查驱动是否正常，通过npu-smi info命令是否执行成功判断，2、查看npu-smi '
                                    'info回显，判断状态是否有非OK的卡。',
                         'error_msg': 'err msg',
                         'help_url': '',
                         'tip_en': '',
                         'tip_zh': ''}
        self.assertEqual(expect_result, self.check_output.to_json())

    def test_get_formatted_error_msg(self):
        self.check_output.error_msg = []
        self.assertEqual("", self.check_output.get_formatted_error_msg())
        self.check_output.error_msg = ['err msg']
        self.assertEqual('err msg', self.check_output.get_formatted_error_msg())
        self.check_output.error_msg = ['err msg1', 'err msg2']
        self.assertEqual("1. err msg1 2. err msg2", self.check_output.get_formatted_error_msg())
